// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:'',
    curr:'home_search_tabs1_block',
    userData:{},
    user:'{"tx":"https://thirdwx.qlogo.cn/mmopen/vi_32/vYOPewuv6LjvPibudDrrB9dRDUwGqV7qfoibIXGrwyHNwVgribzibJaFf18YBkGicqNQaZa8WceoWMNzZJeCcq86EXg/132","name":"『Silence』&#128347;"}',
    historyList:[],
    hotTitle:[
      {
        name:'牛蒡满满暖心猪肉汤',
        browse:'231.6万人浏览',
        type:1
      },
      {
        name:'简易版腌鲑鱼白汁意大利面',
        browse:'194.3万人浏览',
        type:1
      },
      {
        name:'菌菇鲜虾冬阴功锅',
        browse:'172.1万人浏览',
        type:1
      },
      {
        name:'梅香白萝卜猪肉锅',
        browse:'170.5万人浏览'
      },
      {
        name:'豆腐菌菇鸡肉寿喜锅',
        browse:'143.8万人浏览'
      },
      {
        name:'猪肉挂面涮涮锅',
        browse:'132.5万人浏览'
      }
    ],
    searchList:[]
  },
  getData(){
    const that=this;
    wx.request({
      url: 'http://localhost:8888/gethistory',
      data:{name:JSON.parse(that.data.user).name},
      success:(res)=>{
        console.log(res.data)
          if(res.data.data[0].username==JSON.parse(that.data.user).name){
            that.setData({
              userData:res.data.data[0],
              historyList:res.data.data[0].history?JSON.parse(res.data.data[0].history):[]
            })
            console.log(that.data.userData)
          }
      }
    })
  },
  input(e){
    this.setData({
      search:e.detail.value
    })
  },
  search(){
    let _this=this;
    let {id} = _this.data.userData;
    let history = this.data.historyList
    history.push(_this.data.search)
    history = JSON.stringify(history)
    console.log(history)
    wx.request({
      url: 'http://localhost:8888/searchHistory',
      method:'POST',
      data:{id,history},
      success:(res)=>{
        console.log(res.data)
        if(res.data.code==200){
          _this.setData({
            search:''
          })
          _this.getData()
        }
      }
    })
    let {search} = _this.data;
    wx.request({
      url: 'http://localhost:8888/search',
      method:'POST',
      data:{search},
      success:(res)=>{
        console.log(res.data.data)
        if(res.data.data.length>0){
          _this.setData({
            curr:'home_search_tabs2_block',
            searchList:res.data.data
          })
        }
      }
    })
    console.log(_this.data.searchList)
  },
  toDetails(e){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:wx.getStorageSync('user')
    })
    let that=this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})